package com.im.sso.service.jwtsecurity;

import com.im.sso.entity.User;
import com.im.sso.entity.security.JwtUserDetails;
import com.im.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @Author : KingFish
 * @Email : XXXXXX
 * @Data: 2019/10/30
 * @Des:
 */
@Component
public class JwtUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectByName(username);
        if (null == user) {
            return null;
        }
        JwtUserDetails jwtUserDetails = new JwtUserDetails();
        jwtUserDetails.setUserId(user.getId());
        jwtUserDetails.setUserName(username);
        jwtUserDetails.setUserPwd(user.getPassword());
        jwtUserDetails.setUserEnabled((short) 1);
        return jwtUserDetails;
    }
}